//
//  IGHotPersonCell.m
//  jingdongfang
//
//  Created by BOE on 2018/5/9.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGHotPersonCell.h"
#import "FindCollectionViewCell.h"
#import "IGHotCommnuityCollectionViewCell.h"
#import "IGCommunityModel.h"
#import "IGHotPresonCCell.h"

static NSString *const kIGHotPersonCell = @"kIGHotPersonCell";
@interface IGHotPersonCell ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UIView *layerView;
@property (nonatomic, strong) UIButton *moreBtn;
@property (nonatomic, strong) UILabel *label;

@end

@implementation IGHotPersonCell

+ (IGHotPersonCell *)cellWithTableView:(UITableView *)tableView {
    IGHotPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGHotPersonCell];
    if (!cell) {
        cell = [[IGHotPersonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGHotPersonCell];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self _initViews];
    }
    return self;
}

- (void)_initViews {
    UIView *view = [[UIView alloc] init];
    _view = view;
    view.backgroundColor = [UIColor mainBackgroundColor];
    [self.contentView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
    }];
    
    UIView *layerView = [[UIView alloc] init];
    _layerView = layerView;
    layerView.backgroundColor = [UIColor c3Color];
    [self.contentView addSubview:layerView];
    [layerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
    }];
    
    UILabel *label = [UILabel new];
    _label = label;
    label.hidden = YES;
    label.textColor = [UIColor blackColor];
    label.font = [UIFont font16];
    [layerView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(200);
    }];
    
    label.text = NSLocalizedString(@"Hot Person", nil);

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 3;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor c3Color];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[IGHotPresonCCell class] forCellWithReuseIdentifier:@"kIGHotPresonCCell"];
    [self.contentView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_bottom);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
    }];
}

- (void)setHotPersonArr:(NSArray *)hotPersonArr {
    _hotPersonArr = hotPersonArr;
    
    if (_hotPersonArr.count > 0) {
        _label.hidden = NO;
        [_view mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
        }];
        
        [_layerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(6.5);
            make.height.mas_equalTo(43.5);
        }];
        
        [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(88);
        }];
        
        [self.collectionView reloadData];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.hotPersonArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    IGHotPresonCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kIGHotPresonCCell" forIndexPath:indexPath];
    
    IGCommunityModel *model = self.hotPersonArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(48.5, 88);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
